# Top Makefle for Gauche
#  Run 'configure' script to generate Makefile

.PHONY: all test check pre-package install uninstall \
	clean distclean maintainer-clean install-check \
	rpmfiles

@SET_MAKE@
SHELL       = @SHELL@
prefix      = @prefix@
exec_prefix = @exec_prefix@
bindir      = @bindir@
libdir      = @libdir@
srcdir      = @srcdir@
datadir     = @datadir@
datarootdir = @datarootdir@
top_srcdir  = @top_srcdir@

SUBDIRS = gc src lib ext doc examples
SRIDBUS = examples doc ext lib src gc
LIBATOMICDIR = gc/libatomic_ops
MKINSTDIR    = $(top_srcdir)/mkinstalldirs
INSTALL      = @INSTALL@
INSTALL_DATA = @INSTALL_DATA@
GAUCHE_VERSION  = @GAUCHE_VERSION@
GAUCHE_DATA_DIR = $(datadir)/gauche-@GAUCHE_ABI_VERSION@
GAUCHE_ARCH_DIR = $(libdir)/gauche-@GAUCHE_ABI_VERSION@
GAUCHE_ACLOCAL_DIR = $(GAUCHE_DATA_DIR)/$(GAUCHE_VERSION)/
BUILD_GOSH_FLAGS =
DESTDIR =

GOSH = gosh
LN_S = @LN_S@

PRE_PACKAGE_GENERATED = INSTALL.en.adoc INSTALL.ja.adoc
CONFIG_GENERATED = Makefile config.cache config.log config.status \
	           libtool configure.lineno autom4* gc/autom4* \
	           gc/configure.gnu-gauche Gauche.spec
AUTOCONF_GENERATED = $(CONFIG_GENERATED) configure

TESTRECORD = ./test.record

all:
	if [ $(srcdir) != "." ]; then $(srcdir)/wirebuildlibs "$(srcdir)" "$(LN_S)"; fi
	for d in $(SUBDIRS); do (cd $$d; $(MAKE) all) || exit 1; done
	cd src; $(MAKE) -s static
	@@CYGWIN_FIXDLL@

test : check

check: all
	@rm -f $(TESTRECORD)
	set -e; for d in $(SUBDIRS); do (cd $$d; $(MAKE) check); done
	@cd src; $(MAKE) test2
	@cat $(TESTRECORD)
	@cd src; $(MAKE) test-summary-check

install-check:
	@echo "Testing installed Gauche"
	@rm -f test.log $(TESTRECORD)
	@(cd src; $(MAKE) install-check) >> test.log
	@(cd ext; $(MAKE) install-check) >> test.log
	@cat $(TESTRECORD)
	@cd src; $(MAKE) test-summary-check

# pre-package target should be run before creating distribution tarball.
# it generates files that requires pre-installed gosh.
pre-package: $(PRE_PACKAGE_GENERATED)
	cd src; $(MAKE) GOSH=$(GOSH) BUILD_GOSH_FLAGS=$(BUILD_GOSH_FLAGS) pre-package
	cd ext; $(MAKE) GOSH=$(GOSH) BUILD_GOSH_FLAGS=$(BUILD_GOSH_FLAGS) pre-package
	cd doc; $(MAKE) GOSH=$(GOSH) BUILD_GOSH_FLAGS=$(BUILD_GOSH_FLAGS) pre-package

INSTALL.en.adoc: INSTALL.adoc
	sed -e '/^ifdef::JA/,/^endif::JA/d ; /^ifdef::EN/d ; /^endif::EN/d ; /^\/\/@start header/,/^\/\/@end header/d' $< > $@

INSTALL.ja.adoc: INSTALL.adoc
	sed -e '/^ifdef::EN/,/^endif::EN/d ; /^ifdef::JA/d ; /^endif::JA/d ; /^\/\/@start header/,/^\/\/@end header/d' $< > $@

install: install-pkg install-doc
	cd lib; $(MAKE) slibcat

install-doc:
	cd doc; $(MAKE) install

# it is important to install lib and ext _before_ src, since they
# depend on gosh's rpath to point a valid version of libgauche.so.
install-pkg:
	cd lib; $(MAKE) install
	cd ext; $(MAKE) install
	cd src; $(MAKE) install
	$(INSTALL_DATA) "$(srcdir)/m4/gauche.m4" "$(DESTDIR)$(GAUCHE_ACLOCAL_DIR)/aclocal.m4"
	$(MKINSTDIR) "$(DESTDIR)$(datadir)/aclocal"
	$(INSTALL_DATA) "$(srcdir)/m4/gauche.m4" "$(DESTDIR)$(datadir)/aclocal/gauche.m4"

slibcat-in-place:
	cd lib; $(MAKE) slibcat-in-place

uninstall:
	cd src; $(MAKE) uninstall
	cd ext; $(MAKE) uninstall
	cd lib; $(MAKE) uninstall
	cd doc; $(MAKE) uninstall
	rm -f $(GAUCHE_ACLOCAL_DIR)/aclocal.m4
	rm -f $(datadir)/aclocal/gauche.m4

# Install selected examples.  We don't usually install examples.  For Windows
# we distribute binary, so it's useful to include some examples in it.
install-examples:
	$(INSTALL) -d "$(DESTDIR)${prefix}/examples"
	$(INSTALL) -d "$(DESTDIR)${prefix}/examples/windows"
	$(INSTALL_DATA) examples/windows/*.scm "$(DESTDIR)${prefix}/examples/windows"

# Cleaning ---------------------------------------------
#  NB: we don't run maintainer-clean in $(LIBATOMICDIR) to avoid
#      dealing with automake.
clean:
	rm -rf test.log test.record core Gauche.framework rpmfiles-*.txt *~
	-for d in $(SRIDBUS); do (cd $$d; $(MAKE) clean); done
	-if test -f $(LIBATOMICDIR)/Makefile; then (cd $(LIBATOMICDIR); $(MAKE) clean); fi

distclean: clean
	-for d in $(SRIDBUS); do (cd $$d; $(MAKE) distclean); done
	-if test -f $(LIBATOMICDIR)/Makefile; then (cd $(LIBATOMICDIR); $(MAKE) distclean); fi
	-(cd winnt/wix; $(MAKE) clean)
	rm -rf $(CONFIG_GENERATED)

maintainer-clean: clean
	-for d in $(SRIDBUS); do (cd $$d; $(MAKE) maintainer-clean); done
	-if test -f $(LIBATOMICDIR)/Makefile; then \
	   (cd $(LIBATOMICDIR); $(MAKE) distclean; rm -f configure);\
	fi
	-(cd winnt/wix; $(MAKE) clean)
	rm -rf $(AUTOCONF_GENERATED) VERSION DIST_EXCLUDE_X \
	    gc/configure gc/configure.gnu \
	    $(PRE_PACKAGE_GENERATED)

# MacOSX Framework specific stuff
framework: all
	./framework.sh $(GAUCHE_VERSION)

# Other dummy targets -------------------------------------------

dist:
	@echo "To create a distribution tarball, use DIST script."

distcheck:
	@echo "To create a distribution tarball, use DIST script."

# This must be called after the current version of Gauche is installed
rpmfiles:
	find $(GAUCHE_DATA_DIR)/$(VERSION) -type f -print | grep -v 'dbm/[ong]dbm' > rpmfiles-common.txt
	find $(GAUCHE_DATA_DIR)/$(VERSION) -type f -print | grep 'dbm/[ong]dbm' > rpmfiles-gdbm.txt
	find $(GAUCHE_ARCH_DIR)/$(VERSION) -type f -print | grep -v '/dbm--[ong]dbm' > rpmfiles-encoding.txt
	find $(GAUCHE_ARCH_DIR)/$(VERSION) -type f -print | grep '/dbm--[ong]dbm' >> rpmfiles-gdbm.txt

